#include "sem.h"

Semaphore::Semaphore():
    good(false) {
}

bool Semaphore::init(int val) {
    if(!psem.allocate()) {
        return false;
    }

    good = (sem_init(psem, 1, val) != -1);

    return good;
}

void Semaphore::wait() {
	while(sem_wait(psem)==-1 && errno==EINTR);
}

void Semaphore::signal() {
	sem_post(psem);
}

void Semaphore::destroy() {
    good=false;
	sem_destroy(psem);
	psem.free();
}

int Semaphore::getValue() {
	int val;
	sem_getvalue(psem, &val);
	return val;
}

Semaphore::operator bool() {
	return good;
}
